package com.hoshiibuy.global.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.global.entity.SysRelationshipsAttr;
import com.hoshiibuy.global.feign.PlatformService;
import com.hoshiibuy.global.mapper.SysRelationshipsAttrMapper;
import com.hoshiibuy.global.service.SysLabelsService;
import com.hoshiibuy.global.service.SysPropertiesService;
import com.hoshiibuy.global.service.SysRelationshipsAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.global.service.SysRelationshipsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.global.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * 关系属性表 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-04-27
 */
@Service
public class SysRelationshipsAttrServiceImpl extends ServiceImpl<SysRelationshipsAttrMapper, SysRelationshipsAttr> implements SysRelationshipsAttrService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private SysRelationshipsService sysRelationshipsService;
    @Autowired
    private SysPropertiesService sysPropertiesService;

    /**
    * @param id
    * @return com.hoshiibuy.global.entity.SysRelationshipsAttr
    * @description: 根据id查询关系属性表
    */
    @Cacheable(cacheNames = "SysRelationshipsAttr", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "全局基本定义-关系属性表 -查询关系属性表 ")
    @Override
    public SysRelationshipsAttr getById(Long id) {
        return baseMapper.selectById(id);
    }

    //根据关系id查询数据
    public SysRelationshipsAttr getByRid(Long rid){
        Wrapper wrapper = Wrappers.<SysRelationshipsAttr>query().lambda().eq(SysRelationshipsAttr::getRid, rid);
        return baseMapper.selectOne(wrapper);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增关系属性表
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SysRelationshipsAttr", allEntries = true)
    @Logable(authName = "全局基本定义-关系属性表-新增关系属性表")
    @Override
    public boolean save(SysRelationshipsAttr entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        Response responseR=Response.data(sysRelationshipsService.getById(entity.getRid()));
        if(responseR.getCode() != 200 || responseR.getData() == null){
            throw new ServiceException(CommonError.error("新增失败,关系信息有误"));
        }
        Response responseP=Response.data(sysPropertiesService.getById(entity.getAttrId()));
        if(responseP.getCode() != 200 || responseP.getData() == null){
            throw new ServiceException(CommonError.error("新增失败,属性信息有误"));
        }
        SysRelationshipsAttr sRA=getByRid(entity.getRid());
        if (sRA!=null){
            if (sRA.getAttrId().toString().equals(entity.getAttrId().toString())){
                throw new ServiceException(CommonError.error("新增失败,此关系属性已存在"));
            }
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            System.out.println(row);
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改关系属性表
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SysRelationshipsAttr", allEntries = true)
    @Logable(authName = "全局基本定义-关系属性表管理-修改关系属性表")
    @Override
    public boolean updateById(SysRelationshipsAttr entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        SysRelationshipsAttr sRA=getByRid(entity.getRid());
        if (sRA!=null){
            if (sRA.getAttrId().toString().equals(entity.getAttrId().toString())){
                throw new ServiceException(CommonError.error("修改失败,此关系属性已存在"));
            }
        }
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
        throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询关系属性表
    */
    @Logable(authName = "全局基本定义-关系属性表管理-分页查询关系属性表")
    @Cacheable(cacheNames = "SysRelationshipsAttr", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
    @Override
    public IPage<SysRelationshipsAttr> page(IPage<SysRelationshipsAttr> page, Wrapper<SysRelationshipsAttr> queryWrapper) {
    return baseMapper.selectPage(page, queryWrapper);
    }
}
